package com.example.wordcardjavaproject.controller;

import com.example.wordcardjavaproject.bean.entity.Word;
import com.example.wordcardjavaproject.bean.form.QueryWord;
import com.example.wordcardjavaproject.service.WordService;

import com.example.wordcardjavaproject.utils.SecurityUserIdUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
public class WordController {

    @Resource
    WordService wordService;

    @RequestMapping(value = "/getWordList", method = RequestMethod.POST)
    public Object getWordList(@RequestBody(required = false) QueryWord data, @RequestHeader MultiValueMap<String, String> headers) {
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 200);
        resultMap.put("data", wordService.selectWordList(data));
        return resultMap;
    }

    @RequestMapping(value = "/updateWord", method = RequestMethod.POST)
    public Object updateWord(@RequestBody Word data) {
        wordService.updateWord(data);
        HashMap<Object, Object> resultMap = new HashMap<>();
        resultMap.put("code", 200);
        resultMap.put("msg", "OK");
        return resultMap;
    }

    @RequestMapping(value = "/addWordByFile", method = RequestMethod.POST)
    public Object addWordByFile(@RequestBody MultipartFile file, Long userId) throws Exception {
        System.out.println(file);
        wordService.addWordByFile(file, SecurityUserIdUtils.getUserId());
        HashMap<Object, Object> resultMap = new HashMap<>();
        resultMap.put("code", 200);
        resultMap.put("msg","文件上传成功！");
        return resultMap;
    }

    @RequestMapping(value = "/addWordByWeb", method = RequestMethod.POST)
    public Object addWordByWeb(@RequestBody Word data) {
        wordService.addWordByWeb(data);
        HashMap<Object, Object> resultMap = new HashMap<>();
        resultMap.put("code", 200);
        return resultMap;
    }

    @RequestMapping(value = "/deleteWord", method = RequestMethod.POST)
    public Object deleteWord(@RequestBody Word word) {
        wordService.deleteWord(word);
        Map<Object, Object> map = new HashMap<>();
        map.put("code", 200);
        return map;
    }
}